<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ShopsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $shops = DB::table('shops')
        ->join('shopers','shopers.id','=','shops.spid')
        ->select('shops.*','shopers.shopername')
        ->get();

        // dd($shops);
        // $shops = DB::table('shops')->get();
        return view('admin.shops.shopsList',['shops'=>$shops]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        echo 666;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $shops = DB::table('shops')
        ->join('shopers','shopers.id','=','shops.spid')
        ->select('shops.*','shopers.shopername')
        ->where('shops.id', $id)
        ->first();
        // dd($shops);
        // dd($shops);
        if(!empty($shops)){
            return view('admin.shops.edit', ['shops'=>$shops]);
        }
        return back();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($_POST);
        $res = DB::table('shops')
            ->where('id', $id)
            ->update(['spid' => $_POST['spid'],
            'sptype' =>  $_POST['sptype'],
            'income' =>  $_POST['income'],
            'shopname' =>  $_POST['shopname'],
            'logo' =>  $_POST['logo'],
            'shopstatus' =>  $_POST['shopstatus'],
            'address' =>  $_POST['address'],
            'content' =>  $_POST['content']]
            );
            // dd($res);
            if(!empty($res)){
                return redirect('admin/shops');
            }

            return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = DB::table('shops')->where('id',$id)->delete();
        return back();
    }
}
